How to detect when a presented AVPlayerViewController is dismissed

I think what I'm trying to do is fairly standard, I'm creating an AVPlayerViewController then presenting it from my view controller, so that it will open modally and allow the user to play a video.


When presented modally, the AVPlayerViewController will helpfully show a 'Done' button which allows the user to dismiss the modal when they are finished watching. But there doesn't seem to be any way to detect when this dismissal happens.


This is particularly frustrating because the PiP documentation says that in order to support Picture-in-Picture with AVPVC you must

"Ensure your app’s audio session employs an appropriate category, such as AVAudioSessionCategoryPlayback."


So I want to activate the audio session, present the player VC, then when it gets dismissed, deactivate the audio session. I've tried everything I can think of to detect this:

  • AVPlayerViewController doesn't notify it's delegate that it wants to be dismissed, unlike other VCs designed for presentation
  • According to the docs, we are forbidden from subclassing AVPlayerViewController, so I can't override viewWillDisappear or anything like that
  • The VC underneath doesn't receive a viewDidAppear when it is uncovered by the modal
  • I even tried KVO on my presentedViewController property, but I guess it's not observable


What am I doing wrong here? 🙂

Post not yet marked as solved Up vote post of glennsc Down vote post of glennsc
5.3k views

Replies

In my case I pause playback in viewWillDisappear: of my AVPlayerViewController subclass. Works fine.

What about when you dismiss the AVPlayerViewController to hide it when PIP starts? viewWillDisappear: will be called when you do this.